﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text





Partial Public Class CustomerService

    Public Shared Function ReadItem(ByVal customerID As String) As Customer
        Dim context As New CustomerDataContext

        Dim cust = (From c In context.Customers _
                   Where c.CustomerID = customerID _
                   Select c).Single()
        Return cust
    End Function

    Public Shared Function ReadList() As IEnumerable(Of Customer)
        Dim context As New CustomerDataContext

        Return context.Customers
    End Function

    Public Shared Function Create(ByVal newCustomer As Customer) As Customer
        Dim context As New CustomerDataContext

        context.Customers.InsertOnSubmit(newCustomer)
        context.SubmitChanges()

        Dim cust = (From c In context.Customers _
                   Where c.CustomerID = newCustomer.CustomerID _
                   Select c).Single()

        Return cust
    End Function

    Public Shared Sub Delete(ByVal customerID As String)
        Dim context As New CustomerDataContext

        Dim cust = (From c In context.Customers _
                   Where c.CustomerID = customerID _
                   Select c).Single()

        context.Customers.DeleteOnSubmit(cust)
        context.SubmitChanges()

    End Sub

    Public Shared Sub Update(ByVal customer As Customer, ByVal customerID As String)
        Dim context As New CustomerDataContext

        Dim cust = (From c In context.Customers _
                   Where c.CustomerID = customer.CustomerID _
                   Select c).Single()

        cust.Address = customer.Address
        cust.City = customer.City
        cust.CompanyName = customer.CompanyName
        cust.ContactName = customer.ContactName
        cust.ContactTitle = customer.ContactTitle
        cust.Country = customer.Country
        cust.Fax = customer.Fax
        cust.Phone = customer.Phone
        cust.PostalCode = customer.PostalCode
        cust.Region = customer.Region

        context.SubmitChanges()

    End Sub
End Class
